<?php
	include("Base.php");
	class Role extends Base{
		//得到角色列表
		public function getRoleList(){
			$db=new DB();
			$sql="SELECT * from chu_role";
			$roles=$db->query($sql);
			$db->close();
			return $roles;
		}

		//得到所有菜单
		public function getAllMenu($id,$name){
			$db=new DB();
			//得到原有权限
			$access=$db->query("SELECT menu_id from chu_role_menu where role_id=".$id);
			$arr=array();
			foreach ($access as $v) {
				$arr[]=$v['menu_id'];
			}
			$sql="SELECT * from chu_menu order by level";
			$menus=$db->query($sql);
			$menus=menu_merge2($menus,$arr);
			$data=array('id'=>$id,'name'=>$name,'menus'=>$menus);
			$db->close();
			return $data;
		}
		//保存新的权限
		public function save($id,$access){
			$db=new DB();
			$delete="DELETE from chu_role_menu where role_id=".$id;
			$db->update($delete);
			foreach ($access as $v) {
				$sql ="Insert into chu_role_menu(role_id,menu_id) value(".$id.",".$v.")";
				$db->update($sql);
			}
			$db->close();
			$this->message("保存成功!")->end();
		}
		//创建新角色
		public function newRole($name,$status){
			$db=new DB();
			if($db->update("Insert into chu_role(name,status) value('".$name."',".$status.")")){
				$data=array("redirectUrl"=>"/Role/rolelist");
				$this->data($data);
				$db->close();
				$this->message("创建成功!")->end();
			}else{
				$this->error("创建失败!")->end();
			}			
		}

		//删除角色
		public function deleteRole($id){
			$db=new DB();
			$sql="SELECT count(*) count from chu_user where role_id=".$id;
			$count=$db->query($sql);
			if($count[0]['count']!=0){
				$db->close();
				$data=array("redirectUrl"=>"/Role/rolelist");
				$this->data($data);
				$this->message("有用户使用了该角色，删除失败")->end();
			}else{
				$sql2="DELETE from chu_role where id=".$id;
				if($db->update($sql2)){
					$db->update("DELETE from chu_role_menu where role_id=".$id);
					$db->close();
					$data=array("redirectUrl"=>"/Role/rolelist");
					$this->data($data);
					$this->message("删除角色成功!")->end();
				}else{
					$db->close();
					$data=array("redirectUrl"=>"/Role/rolelist");
					$this->data($data);
					$this->message("删除失败!")->end();
				}
			}
		}

		//得到用户列表
		public function getUserList($token){
			$db = new DB();
			$user=$db->getOne("SELECT * from chu_user where token='".$token."'");
			$region_id=$user['region_id'];
			$region=$db->getOne("SELECT region_type from chu_region where region_id=".$region_id);
			$region_type=$region['region_type'];
			$sql="";
			if($region_type==1||$region_type==2){
				//得到本级和下一级的管理员
				$sql="SELECT chu_user.uid,chu_user.uname,chu_user.role_id,chu_role.name rolename from chu_user,chu_role where chu_user.Type LIKE '%管理员%' and  chu_user.role_id=chu_role.id and chu_user.region_id in (SELECT region_id from chu_region where region_id=".$region_id." or parent_id=".$region_id.") and uid !=".$user['uid'];
			}
			if($region_type==3){
				$sql="SELECT chu_user.uid,chu_user.uname,chu_user.role_id,chu_role.name rolename from chu_user,chu_role where  chu_user.role_id=chu_role.id and chu_user.region_id in (SELECT region_id from chu_region where region_id=".$region_id." or parent_id=".$region_id.") and uid !=".$user['uid'];
			}
			
			$userList=$db->query($sql);

			$data=array();
			$table=array();
			$table['checkall']=false;
			$thead=array();
			$thead['uname']="用户名";
			$thead['role']="现有角色";
			$table['thead']=$thead;
			$tr=array();
			$i=1;
			foreach ($userList as $value) {
				$arr1=array();
				foreach ($value as $k => $v) {
					$arr1[$k]=$v;				
					
				}
				$tr[$i]=$arr1;
				$i=$i+1;
			}
			$table['tr']=$tr;
			$operate=array();
			$data['table']=$table;
			$data['item_count']="".count($userList);

			$db->close();
			$this->data($data)->end();
		}

		public function getUserRole($uid,$roleid,$token){
			$db=new DB();
			$admin=$db->getOne("SELECT * from chu_user where token='".$token."'");
			$sql="";
			if($admin['Type']=="区管理员"){
				$sql="SELECT id ,name from chu_role where status=1 and name!='省管理员' and name!='市管理员'";
			}else if($admin['Type']=="市管理员"){
				$sql="SELECT id ,name from chu_role where status=1 and name!='省管理员' ";
			}else{
				$sql="SELECT id ,name from chu_role where status=1 ";
			}
			$roles=$db->query($sql);
			//p($roles);die;
			$r=array();
			foreach ($roles as $v) {
				if($v['id']==$roleid)
					$v['active']=true;
				else
					$v['active']=false;
				$r[]=$v;
			}
			$data=array("uid"=>$uid,"roleList"=>$r);
			$db->close();
			$this->data($data)->end();
		}

		public function changeRole($uid,$role_id){
			$db=new DB();
			$role=$db->getOne("SELECT name from chu_role where id=".$role_id);
			$sql="update chu_user set role_id=".$role_id." ,Type='".$role['name']."' where uid=".$uid;
			//p($sql);die;
			if($db->update($sql)){
				$this->message("更改成功!")->end();
			}else{
				$this->message("更改失败!")->end();
			}
		}

		
	}
?>